﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HOOMT
{
    class InquiryUtil
    {
        //
        public static OriginalModelForm getOriginalModelFormByDiagram(OriginalDiagram diagram)
        {
            OriginalModelForm result = null;

            foreach (OriginalModelForm form in SystemVar.mainForm.modelFormList)
            {
                foreach (OriginalDiagram diag in form.diagramList)
                {
                    if (diagram.id.Equals(diag.id))
                    {
                        foreach (OriginalDiagram origDiag in form.diagramList)
                        {
                            origDiag.IsSelected = false;
                        }
                        diag.IsSelected = true;
                        result = form;
                        break;
                    }
                }
                if (result != null)
                {
                    break;
                }
            }
            return result;
        }
        //
        public static OriginalModelForm getOriginalModelFormByAttr(Attribute attr)
        {
            OriginalModelForm result = null;

            foreach (OriginalModelForm form in SystemVar.mainForm.modelFormList)
            {
                foreach (OriginalDiagram diag in form.diagramList)
                {
                    ObjectDiagram objectDiag = (ObjectDiagram)diag;
                    foreach (Attribute newAttr in objectDiag.AttributeList)
                    {
                        if (attr.id.Equals(newAttr.id))
                        {
                            foreach (OriginalDiagram origDiag in form.diagramList)
                            {
                                origDiag.IsSelected = false;
                            }
                            objectDiag.IsSelected = true;
                            result = form;
                            break;
                        }
                    }
                    if (result != null)
                    {
                        break;
                    }
                }
                if (result != null)
                {
                    break;
                }
            }

            return result;
        }
        //
        public static OriginalModelForm getOriginalModelFormByMethod(Method method)
        {
            OriginalModelForm result = null;

            foreach (OriginalModelForm form in SystemVar.mainForm.modelFormList)
            {
                foreach (OriginalDiagram diag in form.diagramList)
                {
                    ObjectDiagram objectDiag = (ObjectDiagram)diag;
                    foreach (Method newMethod in objectDiag.MethodList)
                    {
                        if (method.id.Equals(newMethod.id))
                        {
                            foreach (OriginalDiagram origDiag in form.diagramList)
                            {
                                origDiag.IsSelected = false;
                            }
                            objectDiag.IsSelected = true;
                            result = form;
                            break;
                        }
                    }
                    if (result != null)
                    {
                        break;
                    }
                }
                if (result != null)
                {
                    break;
                }
            }

            return result;
        }
        //通过面板得到其所属的高阶对象
        public static HighOrderObjectDiagram getHighOrderObjectByNextLevelModelForm(OriginalModelForm form)
        {
            HighOrderObjectDiagram result = null;
            foreach (OriginalModelForm tmpForm in SystemVar.mainForm.modelFormList)
            {
                foreach (OriginalDiagram diag in tmpForm.diagramList)
                {
                    if(diag is HighOrderObjectDiagram &&((HighOrderObjectDiagram)diag).HasModelForm&&((HighOrderObjectDiagram)diag).NewModelForm.id.Equals(form.id))
                    {
                        result = (HighOrderObjectDiagram)diag;
                        break;
                    }
                }
                if (result != null)
                {
                    break;
                }
            }
            return result;
        }
        //
        public static OriginalModelForm getOriginalModelFormByObjectDiagram(ObjectDiagram diagram)
        {
            OriginalModelForm result = null;
            foreach (OriginalModelForm tmpForm in SystemVar.mainForm.modelFormList)
            {
                foreach (OriginalDiagram tepDiag in tmpForm.diagramList)
                {
                    if (tepDiag.id.Equals(diagram.id))
                    {
                        result = (OriginalModelForm)tmpForm;
                        break;
                    }
                }
                if (result != null)
                {
                    break;
                }
            }
            return result;
        }
        //
        //public static bool isTopLevelModelForm(OriginalModelForm modelForm)
        //{
        //    bool result = true;
        //    foreach (OriginalModelForm tmpForm in SystemVar.mainForm.modelFormList)
        //    {
        //        foreach (OriginalDiagram tmpDiag in tmpForm.diagramList)
        //        {
        //            if (tmpDiag is HighOrderObjectDiagram && ((HighOrderObjectDiagram)tmpDiag).HasModelForm && ((HighOrderObjectDiagram)tmpDiag).ModelForm.id.Equals(modelForm.id))
        //            {
        //                result = false;
        //                break;
        //            }
        //        }
        //        if (!result)
        //        {
        //            break;
        //        }
        //    }
        //    return result;
        //}
    }
}
